package com.dingreading.cloud.subject.service.impl;

import com.dingreading.cloud.subject.entity.CourseVideo;
import com.dingreading.cloud.subject.entity.table.CourseVideoTableDef;
import com.dingreading.cloud.subject.mapper.CourseVideoMapper;
import com.dingreading.cloud.subject.service.CourseVideoService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CourseVideoServiceImpl extends BaseServiceImpl<CourseVideoMapper, CourseVideo, CourseVideoTableDef> implements CourseVideoService {

    @Override
    protected CourseVideoTableDef getTable() {
        return CourseVideoTableDef.courseVideo;
    }


    @Override
    public List<CourseVideo> getByCourseId(Long courseId, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.courseId.eq(courseId));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());
        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public void updateLookNum(Long id) {
        UpdateChain.of(CourseVideo.class).set(table.lookNum, table.lookNum.add(1)).where(table.id.eq(id)).update();
    }

}
